SVGPath

Part of wslib

see also: SVG, SVGFile

superclass : SVGObject

related classes : SVGPathSegment

The SVGPath object contains data of the most complex and powerful SVG object: the path. An SVGPath is constructed out of SVGPathSegment objects, wich each describe one step in the path.

The SVG plotting option dosn't yet display all known types of SVGPathSegments.

creation

SVGPath ( segments, name, strokeColor, fillColor, strokeWidth, transform )

creates a new object with the instance variables as described below and in the SVGObject helpfile

segments : segments is an Array of objects wich should all respond to the asSVGPathSegment method.

these can be:

SVGPathSegment

Point : will be converted to an SVGPathSegment of type 'LineTo', or will be added to

the previous item if that has less arguments then it can have

Array : [ type, ...values ] where type should be a Symbol.

If type starts with an upper case char, the segment is absolute, lowercase is relative to the

previous segment. See the SVGPathSegment helpfile for details about types.

SVGPath internally cleans up the array, which makes the input more flexible.

segments can also be a String, formatted according to the standard SVG 1.1 implementation

(

SVGPath( [

[ \MoveTo, 10, 10 ],

100@100, // becomes 'LineTo' in this case

[ \CurveTo, 150, 150, 150, 250, 100, 300 ],

[ \closePath ] ],

"my path", "purple" ).plot;

)

( // same thing:

SVGPath( [ [ \M, 10, 10 ], [ \L, 100, 100 ], [ \C, 150, 150, 150, 250, 100, 300 ], [ \z ] ],

"my path", "purple" ).plot;

)

// same thing:

SVGPath( [ \M, 10@10, \L, 100@100, \C, 150@150, 150@250, 100@300, \z ], "my path", "purple" ).plot;

// same thing:

SVGPath( "M10,10 L100,100 C150,150 150,250 100,300z", "my path", "purple" ).plot;

( // same thing:

SVGPath( [

SVGPathSegment( \MoveTo, 10, 10 ),

SVGPathSegment( \LineTo, 100, 100 ),

SVGPathSegment( \CurveTo, 150, 150, 150, 250, 100, 300 ),

SVGPathSegment( \closePath ) ],

"my path", "purple" ).plot;

)

(

SVGPath( [[ \M, 200, 200 ]] ++ ({ [ \sCurveTo ] ++ ({ 50.rand2 }!4); }!10), "randomwalk",

strokeColor: "dark_blue" ).plot;

)

(

SVGPath( [[\M,200,200]] ++ ({|i|[[ \HLineTo, \VLineTo ].wrapAt(i), 400.rand] }! 50), "rectrand",

strokeColor: "yellow",

fillColor: "navy" ).plot

)

asSVGPath

this message can be called on a number of classes:

Collection, String

uses the Collection as segments argument

[[ \M, 10, 10 ],[ \l, 200, 20 ],[ \c, 10,100, -100, 10, -100, 50 ],[ \z ]].asSVGPath.plot;

SVGCircle, SVGEllipse, SVGLine, SVGPolyLine, SVGPolygon, SVGRect

for these classes an extra argument "relative" is available, which defaults to 'true'. If relative is true

all path segments are relative to the first \MoveTo element

(

r = SVGEllipse( 120, 100, 100, 80, strokeColor: "blue" );

p = r.asSVGPath( true ).strokeColor_( "red" );

p[0].xy = p[0].xy + [10,10]; // changing the first element moves the whole path (relative)

SVGGroup( [ r, p ] ).plot;

p.asArray.dopost; // post the segments as an array

)

instance variables

segments

an Array with SVGPathSegment objects, describing the segments of the path

strokeColor, fillColor

colors for stroke and fill. These can be

a Color

a String or Symbol with a color name ( e.g. "light_blue" or "#0000FF" )

"none" or nil

strokeWidth

defaults to 1.

instance methods

absoluteSegments

returns segments converted to absolute segments

add ( segment ), addAll ( segmentsArray )

add SVGPathSegments to the segments

++ anSVGPath

Concatenate the segments of 2 SVGPaths to the first SVGPath.

at ( index ), copySeries ( first, second, last ), put (index, item )

calls .at and copySeries on points, also to provide compatibility with the ..[..] syntax in SC

asXYArray

returns an array containing arrays with x and y values

asArray

returns an with SVGPathElement arrays, in a format which could be used as input for SVGPath.

SVGPath( [[ \M, 200, 200 ]] ++ ( { [ \sCurveTo ] ++ ({ 50.rand2 }!4); }!10) ).asArray;